--[[
    @brief    2-fa tips
    @author   senpingtian
    @date     2019-07-18
--]]

login_verify_tips = {
    
    kRootCtrlPath = "CtnVerifyTips";

    kBindConfigURL = LoadStr("QUERY_BIND_TIPS_URL");

    parent_ctrl_id = Tenio.INVALID_CONTROLID;

    root_ctrl_id = Tenio.INVALID_CONTROLID;
    root_ctrl_ptr = nil;

    ref_ctrl_id = Tenio.INVALID_CONTROLID;
    ref_ctrl_ptr = nil;

    margin_top = 0;
    margin_right = 0;

    init = function(parent_ctrl)
        base_utility.log("[login_verify_tips] init.");
        local id = ui_utility.get_ctrl_id_by_path(parent_ctrl, login_verify_tips.kRootCtrlPath);
        login_verify_tips.root_ctrl_id = id;
        login_verify_tips.root_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(id);
        login_verify_tips.parent_ctrl_id = parent_ctrl;
        local tips_text = LoadStr("LOGIN_VERIFY_BIND_TIPS");
        -- insert 'space' to draw icon
        local ww_icon, wh_icon = ui_utility.get_control_size_by_path(login_verify_tips.root_ctrl_id,
                                                                    "RtbContent\\CtnIcon");
        local spaces = " ";
        for index = 1, 10 do
            local width = ui_utility.cal_str_width(spaces);
            if width - 3 > ww_icon then
                break;
             end
             spaces = " "..spaces;
        end
        tips_text = spaces .. tips_text;
        ui_utility.setproperty_by_path(id, "RtbContent", "Caption", tips_text);
        comm_center.listen_broadcast("i_broadcast_mainframe_size_change", login_verify_tips.on_main_frame_size);
    end;

    request_bind_tips_config = function()
        if not base_utility.is_develop_client() then
            return;
        end
        local acc_info = comm_center.call_service("Svr_GetAccountType", {});
        if nil == acc_info then
            base_utility.log("[login_verify_tips] get account info failed.", true);
            return;
        end
        local post_data = {
            account_type = acc_info.account_type or 0;
        };
        base_utility.curl_request_async(login_verify_tips.kBindConfigURL
        , "login_verify_tips.on_query_bind_tips_config" , false, nil
        , true, nil, "p="..base_utility.table_to_json(post_data));
    end;

    on_query_bind_tips_config = function(rsp)
        if nil == rsp then return end;
        base_utility.log("[login_verify_tips]bind config rsp: " .. rsp);
        local rsp_tbl = base_utility.json_to_table(rsp);
        if nil == rsp_tbl then
            return;
        end
        if nil == rsp_tbl.result or 0 ~= rsp_tbl.result then
            return;
        end
        local show_tips = rsp_tbl.show or 0;
        if 1 == show_tips then
            login_verify_tips.show(true);
        end
    end;

    set_reference_control = function(ref_ctrl)
        if Tenio.INVALID_CONTROLID ~= ref_ctrl then
            login_verify_tips.ref_ctrl_id = ref_ctrl;
            login_verify_tips.ref_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(ref_ctrl);
            login_verify_tips.update_margin_by_ref_ctrl(ref_ctrl);
            login_verify_tips.update_position();
        end
    end;

    update_position = function()
        if nil == login_verify_tips.root_ctrl_ptr then
            return;
        end

        local parent_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(login_verify_tips.parent_ctrl_id);
        if nil == parent_ctrl_ptr then
            return;
        end

        local rc_parent = parent_ctrl_ptr:GetRect(FALSE);
        local ww_parent = rc_parent.right - rc_parent.left;
        local wh_parent = rc_parent.bottom - rc_parent.top;

        local rc_tips = login_verify_tips.root_ctrl_ptr:GetRect(FALSE);
        local ww = rc_tips.right - rc_tips.left;
        local wh = rc_tips.bottom - rc_tips.top;
        
        rc_tips.left = ww_parent - login_verify_tips.margin_right;
        rc_tips.top = login_verify_tips.margin_top;
        rc_tips.right = rc_tips.left + ww;
        rc_tips.bottom = rc_tips.top + wh;

        login_verify_tips.root_ctrl_ptr:Move(rc_tips);
    end;

    update_margin_by_ref_ctrl = function(ref_ctrl)
        if nil == login_verify_tips.root_ctrl_ptr then
            return;
        end

        local ref_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(ref_ctrl);
        if nil == ref_ctrl_ptr then
            base_utility.log("[login_verify_tips] reference control null.", true);
            return;
        end
        local parent_ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(login_verify_tips.parent_ctrl_id);
        if nil == parent_ctrl_ptr then
            base_utility.log("[login_verify_tips] parent control null.", true);
            return;
        end

        local rc_parent = parent_ctrl_ptr:GetRect(TRUE);
        local rc_ref = ref_ctrl_ptr:GetRect(TRUE);
        local rc_tips = login_verify_tips.root_ctrl_ptr:GetRect(TRUE);
        
        local ww_parent = rc_parent.right - rc_parent.left;
        local wh_parent = rc_parent.bottom - rc_parent.top;
        local ww_ref = rc_ref.right - rc_ref.left;
        local wh_ref = rc_ref.bottom - rc_ref.top;
        local ww = rc_tips.right - rc_tips.left;
        local wh = rc_tips.bottom - rc_tips.top;

        local rel_left = rc_ref.left - rc_parent.left;
        local rel_top = rc_ref.top - rc_parent.top;
        rc_tips.left = rel_left - (ww - ww_ref) / 2 + 2;
        rc_tips.top = rel_top + wh_ref - 4;
        rc_tips.right = rc_tips.left + ww;
        rc_tips.bottom = rc_tips.top + wh;

        login_verify_tips.margin_top = rc_tips.top;
        login_verify_tips.margin_right = ww_parent - rc_tips.left;
    end;

    is_visible = function()
        if nil == login_verify_tips.root_ctrl_ptr then
            return false;
        end
        if TRUE == login_verify_tips.root_ctrl_ptr:IsVisible() then
            return true;
        end
        return false;
    end;

    show = function(show_tips)
        
        if true == show_tips then
            ui_utility.show_ctrl(login_verify_tips.root_ctrl_id, true);
        else
            ui_utility.show_ctrl(login_verify_tips.root_ctrl_id, false);
        end
    end;

    on_main_frame_size = function()
        login_verify_tips.update_position();
    end;

    on_jump_sec_center = function()
        local url = LoadStr("SELF_SETTING_URL");
        if 0 ~= wegame_mainframe_model.self_tgp_id then
            url = url.."&tgpid="..tostring(wegame_mainframe_model.self_tgp_id);
        end;
        url = url.."&sub_tab=loginsecure";
        local param = {};
        param.type = "jump_url";
        param.id = GAME_ID.GAEM_ID_HOME_PAGE;
        param.url = url;
        param.op_from = 0;
        local json_str = base_utility.table_to_json(param);
        web_utility.open_link(json_str);
        login_verify_tips.show(false);
    end;

    on_click_skip = function()
        login_verify_tips.show(false);
    end;

};